package org.study.clickhouse.api.util;

import com.clickhouse.jdbc.ClickHouseDataSource;

import javax.sql.DataSource;
import java.sql.*;
import java.util.Properties;

/**
 * 类的描述
 *
 * @author Administrator
 * @date 2021-05-19
 */
public class ClickHouseUtils {
    private static final String url = "jdbc:clickhouse://127.0.0.1:8123/default";
    private static final String user = "default";
    private static final String password = "root";

    public static Connection getConnection() throws SQLException {
        try {
            Class.forName("com.clickhouse.jdbc.ClickHouseDriver");
        } catch (ClassNotFoundException e) {
            System.err.println("加载 com.clickhouse.jdbc.ClickHouseDriver 驱动失败！");
        }
        Connection connection = DriverManager.getConnection(url, user, password);
        return connection;
    }

    public static Connection getConnection2() throws SQLException {
        DataSource dataSource = new ClickHouseDataSource(url);
        Connection connection = dataSource.getConnection(user, password);
        return connection;
    }

    public static Connection getConnection3() throws SQLException {
        Properties props = new Properties();
        props.setProperty("user", user);
        props.setProperty("password", password);
        DataSource dataSource = new ClickHouseDataSource(url, props);
        return dataSource.getConnection();
    }
}
